
//** Haaf's Game Engine 1.8

//#include "global.h"

#include "game.h"


hgeSprite * tabs[5];
leftPanel * panel;
saveload * db;
hgeGUISlider *hslider;
hgeGUISlider *vslider;
hgeGUIEditBox * editBox;
hgeGUIButton *bNext;
hgeGUIButton *mSave;
hgeGUIButton *mLoad;
hgeGUIButton *bPrev;
hgeGUIButton *clear;

hgeGUIButton *GroundLayer;
hgeGUIButton *BackLayer;



char *  file_name;
HTEXTURE  textures[5];
//tile * tiles[4096];
tile * tiles[800][600];
	int GridX,GridY; 
float 	x;
float   y;
//////////////////////////////////////
bool init_mass(int _w,int _h)

{
	for(int i=0;i<_w;i++)
      for(int j=0;j<_h;j++)
	  {
       tiles[j][i] = new tile(false,-1,-1,-1,-1,-1,hge);
	  }

	return true;
}
/////////////////////////////////////
void drawTiles(int _w,int _h)
{
  for(int i=0;i<_w;i++)
      for(int j=0;j<_h;j++)
	  {
		  tiles[j][i]->drawTile(hslider->GetValue(),vslider->GetValue());
	  }
}
////////////////////////////////////

void CheckTiles(int _w,int _h)
{
  for(int i=0;i<_w;i++)
      for(int j=0;j<_h;j++)
	  {
		  tiles[j][i]->checkVisibility(hslider->GetValue(),vslider->GetValue());
	  }
}

////////////////////////////////////
void LoadTextures()
{
    textures[0] = hge->Texture_Load("images/frame1.png");
	textures[1] = hge->Texture_Load("images/fence.png");
	textures[2] = hge->Texture_Load("images/scaffold.png");
	textures[3] = hge->Texture_Load("images/crates.png");
	textures[4] = hge->Texture_Load("images/grids_colors.png");
}
///////////////////////////////////////
bool aboveMouse(float _mouseX,float _mouseY,float tileSize)
{
	int mouseX,mouseY;

	 mouseX=_mouseX-160;
     mouseY=_mouseY;
	 GridX=(mouseX+((int)(hslider->GetValue()))*tileSize  )/tileSize   ;
	 GridY=(mouseY+(int)((vslider->GetValue()))*tileSize   )/tileSize   ;
	 
	 if((mouseX<0)||(mouseY<0))
	 {
       GridX=-1;
	   GridY=-1;
	 }

// int n
	//SCREEN_WIDTH/32
	//tileNumber=_mouseX/tileSize;
 return 0;
}

bool FrameFunc()
{
	panel->getTileValue(x,y,32,hge);

	hge->Input_GetMousePos(&x,&y);


	float dt=hge->Timer_GetDelta();
    
	aboveMouse(x,y,32);
	panel->SelectTile(y,x,32);

	static float t=0.0f;

	int id;
	static int lastid=0;

  if (hge->Input_KeyDown(HGEK_LBUTTON)) 

	{
        panel->selectTileOnTileSet();
        
		if((GridX!=-1)||(GridY!=-1))
			tiles[GridY][GridX]= new tile(GroundLayer->GetState(),GridX*32,GridY*32,textures[panel->setNumber],panel->TileNumber,panel->setNumber,hge);
		// tiles[GridY][GridX]->tile_number=panel->TileNumber;

    }


if (hge->Input_KeyDown(HGEK_RBUTTON)) 

	{
        panel->selectTileOnTileSet();
        
		if((GridX!=-1)||(GridY!=-1))
			tiles[GridY][GridX]= new tile(false,-1,-1,-1,-1,-1,hge);
		CheckTiles(100,100);
		// tiles[GridY][GridX]->tile_number=panel->TileNumber;

    }


	// If ESCAPE was pressed, tell the GUI to finish

  if(hge->Input_GetKeyState(HGEK_ESCAPE)) { lastid=5; gui->Leave(); }	
	// We update the GUI and take an action if
	// one of the menu items was selected

//  if(gui->ctrls->bVisible==true)
	  id=gui->Update(dt);
	  
	

     


	
		switch(id)
		{
			case 1:
				//gui->EnableCtrl(1,false);
				 //break;
			case 2:
				  panel->changeTileSet(true);
	             id=-1;
				break;
			case 3:
	               panel->changeTileSet(false);
	               id=-1;
				 break;
			case 4:
				file_name=editBox->GetText();
				db->saveTilesToFile(file_name,100,100,tiles);
				id=-1;
				//lastid=-1;
				//gui->SetFocus(1);
				//gui->Enter();
				break;

			case 5:
				file_name=editBox->GetText();
				db->loadTilesFromFile(file_name,tiles,textures,hge,GroundLayer);
				CheckTiles(100,100);
				id=-1;
				break;

           case 6:
                CheckTiles(100,100);
                break;

           case 7: 
                CheckTiles(100,100);
                break;
 
		    case 9:
				init_mass(100,100);
				CheckTiles(100,100);
				
                break;
 
					case 10:
					  BackLayer->SetState(false); 	
					break;

					case 11:
						GroundLayer->SetState(false);
					break;
			default: break;
			//case 7:
		}
	
//	else if(id) { lastid=id; gui->Leave(); }

	// Here we update our background animation
	
	return false;
}


bool RenderFunc()
{
	// Render graphics
	hge->Gfx_BeginScene();
	//hge->Gfx_RenderQuad(&quad);
	//Resources.GetSprite("spr_bg")->Render(0,0); 
	 //if(gui->ctrls->bVisible==true)
	 


	
    panel->addGridToField(SCREEN_HEIGHT/step,SCREEN_WIDTH/step,hge);
    Resources.GetSprite("spr_grid_vslider")->Render(790,0);
    Resources.GetSprite("spr_grid_hslider")->Render(160,590);
    Resources.GetSprite("spr_grid_slider")->Render(160,160);
	panel->DrawTileSet(panel->setNumber);
	
	gui->Render();
	drawTiles(100,100);
	panel->DrawLines(hge); 
    //Resources.GetSprite("spr_cursor")->Render(0,0);
	Resources.GetFont("fnt")->SetColor(0xFFFFFFFF);
	Resources.GetFont("fnt")->printf(5, 150, HGETEXT_LEFT, "GridX:%d\nGridY:%d\nhValue:%f\nvValue:%f\nvState:%d", GridX, GridY,hslider->GetValue(),vslider->GetValue(),bNext->GetState());
	hge->Gfx_EndScene();

	return false;
}


int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
	
	hge = hgeCreate(HGE_VERSION);

	hge->System_SetState(HGE_LOGFILE, "redactor.log");
	hge->System_SetState(HGE_FRAMEFUNC, FrameFunc);
	hge->System_SetState(HGE_RENDERFUNC, RenderFunc);
	hge->System_SetState(HGE_TITLE, "redactor");
	hge->System_SetState(HGE_WINDOWED, true);
	hge->System_SetState(HGE_SCREENWIDTH, 800);
	hge->System_SetState(HGE_SCREENHEIGHT, 600);
	hge->System_SetState(HGE_SCREENBPP, 32);



	if(hge->System_Initiate())
	{	
		gui=new hgeGUI();
        panel=new leftPanel(hge);
        init_mass(100,100);
        db = new saveload();
		LoadTextures();
/*	tabs[0] = Resources.GetSprite("spr_menu_left_default");
	tabs[1] = Resources.GetSprite("spr_menu_left_colors");
	tabs[2] = Resources.GetSprite("spr_menu_left_particles");
	tabs[3] = Resources.GetSprite("spr_menu_left_effects");
	tabs[4] = Resources.GetSprite("spr_menu_left_textures");
*/		
	//	gui->AddCtrl(new hgeGUIMenuItem(1,Resources.GetFont("fnt"),Resources.GetEffect("eff_menu"),400,200,0.0f,"Play"));
	  //  gui->AddCtrl(new hgeGUIMenuItem(2,Resources.GetFont("fnt"),Resources.GetEffect("eff_menu"),400,240,0.1f,"Options"));
	//	gui->AddCtrl(new hgeGUIMenuItem(4,Resources.GetFont("fnt"),Resources.GetEffect("eff_menu"),400,280,0.2f,"Credits"));
	//	gui->AddCtrl(new hgeGUIMenuItem(5,Resources.GetFont("fnt"),Resources.GetEffect("eff_menu"),400,320,0.3f,"Exit"));
	//	gui->AddCtrl(new hgeGUILabel(6,300,300,Resources.GetFont("fnt"),"tetetetetet",0xFFFFFFFF,1));
	//	gui->AddCtrl(new hgeGUIMemo(7,400,400,20,20,Resources.GetFont("fnt"),"^",0xFFFFFFFF));
		//gui->AddCtrl(new hgeGUIImage(1,0,0,("images/menu2.png")));
		gui->AddCtrl(new hgeGUITabs(1,0,0));
        bNext= new hgeGUIButton(2,10,580,39,14,Resources.GetTexture("tex_next"),0,0);
		bPrev=new hgeGUIButton(3,60,580,39,14,Resources.GetTexture("tex_next"),0,14);
		mSave=new hgeGUIButton(4,10,450,123,27,Resources.GetTexture("tex_savemap"),0,0);
		mLoad=new hgeGUIButton(5,10,500,123,27,Resources.GetTexture("tex_loadmap"),0,0);

		gui->AddCtrl(bNext);
		gui->AddCtrl(bPrev);
		gui->AddCtrl(mSave);
  		gui->AddCtrl(mLoad);



		//gui->AddCtrl(new hgeGUISlider(2,160,0,8,600,Resources.GetTexture("tex_slider"),64,8,64,8,true));
		//gui->AddCtrl(new hgeGUISlider(3,169,592,600,8,Resources.GetTexture("tex_slider"),8,64,8,64,false));



    hslider = new hgeGUISlider(6, 160, 590,640,10,Resources.GetTexture("tex_grid_slider"), 0, 0, 10, 10, false);
	hslider->SetMode(0, 630, HGESLIDER_SLIDER);
	hslider->SetValue(0);
	gui->AddCtrl(hslider);

	//spr_hslider = new hgeSprite(game->hge->Texture_Load("gfx/editorgui/grid_hslider.png"), 0, 0, SCREEN_WIDTH, 10);


	vslider = new hgeGUISlider(7,790,0,10,600,Resources.GetTexture("tex_grid_slider"), 0, 0, 10, 10, true);
	vslider->SetMode(0, 600, HGESLIDER_SLIDER);
	vslider->SetValue(0);
	gui->AddCtrl(vslider);

	//spr_vslider = new hgeSprite(game->hge->Texture_Load("gfx/editorgui/grid_vslider.png"), 0, 0, 10, SCREEN_HEIGHT);



        gui->AddCtrl(editBox=new hgeGUIEditBox(8,10,400,120,Resources.GetFont("fnt"),"text"));
		clear=new hgeGUIButton(9,10,532,123,27,Resources.GetTexture("tex_clearmap"),0,0);
        gui->AddCtrl(clear);
 
		
		GroundLayer=new hgeGUIButton(10,10,560,39,14,Resources.GetTexture("tex_layers"),0,0);
        GroundLayer->SetMode(true); 
        gui->AddCtrl(GroundLayer);

        BackLayer=new hgeGUIButton(11,60,560,39,14,Resources.GetTexture("tex_layers"),0,14);
		BackLayer->SetMode(true); 
        gui->AddCtrl(BackLayer);

        CheckTiles(100,100); 
//hgeGUIEditBox::hgeGUIEditBox(int _id, float x, float y, float w, hgeFont* fnt, char* str)
		//hgeGUIImage(int _id, float x, float y, char* file)
		//hgeGUIMemo(int _id, float x, float y, float w, float h, hgeFont *fnt, char* text, DWORD color);
      // hgeGUILabel(int _id, float x, float y, hgeFont *fnt, char* text, DWORD color, int align)
		gui->SetNavMode(HGEGUI_UPDOWN | HGEGUI_CYCLED);
		gui->SetCursor(Resources.GetSprite("spr_cursor"));
		gui->SetFocus(1);
		gui->Enter();
		
		// Let's rock now!
		hge->System_Start();		
	}

	// Clean up and shutdown



	delete gui;
    Resources.Purge(0);
	hge->System_Shutdown();
	
	hge->Release();
	return 0;
}
